﻿using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;

namespace ForguncySecurityProviderCreator;

public class SpCreatorViewModel : ModelBase
{
    private string _name = "DingTalk";

    public string Name
    {
        get => _name;
        set
        {
            if (_name != value)
            {
                _name = value;
                OnPropertyChanged();
            }
        }
    }

    private string _forguncyInstallationPath = @"C:\Program Files\Forguncy 10";

    public string ForguncyInstallationPath
    {
        get => _forguncyInstallationPath;
        set
        {
            if (_forguncyInstallationPath != value)
            {
                _forguncyInstallationPath = value;
                OnPropertyChanged();
            }
        }
    }

    private string _spSaveFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
        "HZG-SecurityProviders");

    public string SpSaveFolderPath
    {
        get => _spSaveFolderPath;
        set
        {
            if (_spSaveFolderPath != value)
            {
                _spSaveFolderPath = value;
                OnPropertyChanged();
            }
        }
    }

    public string ForguncyWebsitePath { get; set; }
    public Version ForguncyVersion { get; set; }

    public string SpProjectPath { get; set; }
}

public class ModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnBeforeSetProperty<T>(string propertyName, T oldValue, T newValue)
    {
    }

    protected virtual bool SetValue<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (Equals(storage, value)) return false;

        var oldValue = storage;
        OnBeforeSetProperty(propertyName, oldValue, value);
        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }
}